/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.util;

import com.lehe.entry.AceTreeDto;
import com.lehe.entry.AdminMenuDto;

import java.util.*;

/**
 * 类的功能描述
 *
 * @author 王丹
 * @since 2017-03-10
 */
public class AceTreeUtil {

    public static void main(String[] args) {
        AceTreeUtil.testCreateTree();
    }

    public static String biuldTree(List<AdminMenuDto> allMenuList, List<AdminMenuDto> userMenuIds){
        Map<String,AceTreeDto> tree = new LinkedHashMap<String,AceTreeDto>();
        setSelectMenu(allMenuList, userMenuIds);
        tree = createTree(allMenuList);
        String jsonTree = JsonUtil.obj2Json(tree);
        return jsonTree;
    }

    private static void testCreateTree(){
        String jsonTree = biuldTree(createData(), createUserData());
        System.out.println("tree->" + jsonTree);
    }

    private static Map<String, AceTreeDto> createTree(List<AdminMenuDto> menuList){
        Map<String, AceTreeDto> treeMap = new LinkedHashMap<>();
        for(AdminMenuDto menu : menuList){
//            System.out.println(menu.getId() + "--->" + menu.isItemSelected());
            if(menu.getParentId() == 0){
                AceTreeDto aceTreeDto = new AceTreeDto();
                aceTreeDto.setText(menu.getName());
                aceTreeDto.setType(menu.getType());
                AceTreeDto.AdditionalParameters additionalParameters = new AceTreeDto.AdditionalParameters();
                additionalParameters.setId(menu.getId());
                additionalParameters.setItemSelected(menu.isItemSelected());
                additionalParameters.setChildren(getChildren(menu.getId(), menuList));
                aceTreeDto.setAdditionalParameters(additionalParameters);

                treeMap.put(menu.getName(), aceTreeDto);
            }
        }
        return treeMap;
    }

    private static Map<String, AceTreeDto> getChildren(Long parnetId, List<AdminMenuDto> menuList){
        Map<String, AceTreeDto> children = new LinkedHashMap<>();
        for(AdminMenuDto menu : menuList){
            if(parnetId.equals(menu.getParentId())){
                AceTreeDto aceTreeDto = new AceTreeDto();
                aceTreeDto.setText(menu.getName());
                aceTreeDto.setType(menu.getType());
                AceTreeDto.AdditionalParameters additionalParameters = new AceTreeDto.AdditionalParameters();
                additionalParameters.setId(menu.getId());
                additionalParameters.setItemSelected(menu.isItemSelected());
                additionalParameters.setChildren(getChildren(menu.getId(), menuList));
                aceTreeDto.setAdditionalParameters(additionalParameters);

                children.put(menu.getName(), aceTreeDto);
            }
        }
        return children;
    }

    private static void setSelectMenu(List<AdminMenuDto> allMenuList, List<AdminMenuDto> userMenuIds){
        if(null!=userMenuIds && !userMenuIds.isEmpty()){
            allMenuList.stream().forEach(menu -> {
                if(userMenuIds.stream().anyMatch(s -> menu.getId().equals(s.getId()))){
                    menu.setItemSelected(true);
                }
            });
        }
    }

    private static List<AdminMenuDto> createData(){
        List<AdminMenuDto> ret = new ArrayList<>();
        AdminMenuDto a1 = new AdminMenuDto();
        a1.setId(6L);
        a1.setName("用户管理");
        a1.setParentId(0L);
        a1.setType("folder");
        AdminMenuDto a2 = new AdminMenuDto();
        a2.setId(7L);
        a2.setName("财务管理");
        a2.setParentId(0L);
        a2.setType("folder");
        AdminMenuDto a3 = new AdminMenuDto();
        a3.setId(8L);
        a3.setName("平台管理");
        a3.setParentId(0L);
        a3.setType("folder");
        AdminMenuDto a4 = new AdminMenuDto();
        a4.setId(9L);
        a4.setName("玩家管理");
        a4.setParentId(6L);
        a4.setType("folder");
        AdminMenuDto a5 = new AdminMenuDto();
        a5.setId(10L);
        a5.setName("代理管理");
        a5.setParentId(6L);
        a5.setType("folder");
        AdminMenuDto a6 = new AdminMenuDto();
        a6.setId(11L);
        a6.setName("我的玩家");
        a6.setParentId(9L);
        a6.setType("item");
        AdminMenuDto a7 = new AdminMenuDto();
        a7.setId(12L);
        a7.setName("玩家充值");
        a7.setParentId(9L);
        a7.setType("item");
        AdminMenuDto a8 = new AdminMenuDto();
        a8.setId(13L);
        a8.setName("系统用户管理");
        a8.setParentId(8L);
        a8.setType("item");
        AdminMenuDto a9 = new AdminMenuDto();
        a9.setId(14L);
        a9.setName("权限管理");
        a9.setParentId(8L);
        a9.setType("item");
        AdminMenuDto a10 = new AdminMenuDto();
        a10.setId(15L);
        a10.setName("用户设置");
        a10.setParentId(8L);
        a10.setType("item");
        AdminMenuDto a11 = new AdminMenuDto();
        a11.setId(16L);
        a11.setName("我的代理");
        a11.setParentId(10L);
        a11.setType("item");
        AdminMenuDto a12 = new AdminMenuDto();
        a12.setId(17L);
        a12.setName("按钮权限");
        a12.setParentId(0L);
        a12.setType("folder");
        AdminMenuDto a13 = new AdminMenuDto();
        a13.setId(18L);
        a13.setName("充值按钮");
        a13.setParentId(17L);
        a13.setType("item");
        AdminMenuDto a14 = new AdminMenuDto();
        a14.setId(19L);
        a14.setName("其他按钮");
        a14.setParentId(17L);
        a14.setType("item");

        ret.add(a1);
        ret.add(a2);
        ret.add(a3);
        ret.add(a4);
        ret.add(a5);
        ret.add(a6);
        ret.add(a7);
        ret.add(a8);
        ret.add(a9);
        ret.add(a10);
        ret.add(a11);
        ret.add(a12);
        ret.add(a13);
        ret.add(a14);
        return ret;
    }

    private static List<AdminMenuDto> createUserData(){
        List<AdminMenuDto> ret = new ArrayList<>();
        AdminMenuDto a1 = new AdminMenuDto();
        a1.setId(6L);
        AdminMenuDto a2 = new AdminMenuDto();
        a2.setId(13L);
        AdminMenuDto a3 = new AdminMenuDto();
        a3.setId(14L);
        AdminMenuDto a4 = new AdminMenuDto();
        a4.setId(15L);
        AdminMenuDto a5 = new AdminMenuDto();
        a4.setId(18L);

        ret.add(a1);
        ret.add(a2);
        ret.add(a3);
        ret.add(a4);
        ret.add(a5);
        return ret;
    }
}
